package com.xiaoge.controller;


import com.xiaoge.response.Result;
import com.xiaoge.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @作者: 冷俊杰
 * @类名: AliOssController类
 * @创建时间: 2022/8/25 14:09
 * @描述: AliOss的控制器类
 * @修改原图
 * @修改作者
 * @修改时间
 */
@Api(value = "AliOss管理", tags = "AliOss管理")
@RestController
@CrossOrigin
@RequestMapping("/oss")
public class AliOssController {
    @Autowired
    private OssService ossService;

    @GetMapping("/createBucket")
    public String createBucket() {
        ossService.createBucket();
        return "成功";
    }

    @ApiOperation(value = "上传图片文件")
    @PostMapping("/uploadImgFile")
    public Result upload(MultipartFile file) {
        String url = ossService.upload(file);
        return Result.ok().data("url", url);
    }

    @ApiOperation(value = "删除上传替换之后的头像")
    @PostMapping("/deleteImgFile")
    public Result deleteImgFile(String file) {
        try {
            String[] splitFile = file.split(".com/");
            ossService.deleteFile(splitFile[1]);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }
}
